package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import model.FamilyFeatures;
import interfaces.IFamilyFeatures;

public class CFamilyFeatures implements IFamilyFeatures {
  private CConnection db;

  public CFamilyFeatures(CConnection db) {
	this.db = db;
  }
	
  @Override
  public boolean create(FamilyFeatures ff) throws SQLException, ClassNotFoundException {
    Statement s = db.getStatement();
    int i = s.executeUpdate("INSERT INTO Species_Features VALUES (" + ff.getFeatureID() + ", '" + ff.getFamilyName() + "')");
    return i > 0;
  }

  @Override
  public boolean update(FamilyFeatures ff) throws SQLException, ClassNotFoundException {
    return false;
  }
  
  @Override
  public boolean delete(FamilyFeatures ff) throws SQLException, ClassNotFoundException {
    Statement s = db.getStatement();
    int i = s.executeUpdate("DELETE FROM Species_Features WHERE FeatureID = " + ff.getFeatureID() + " AND FamilyName = '" + ff.getFamilyName() + "'");
    return i > 0;
  }

  @Override
  public ArrayList<FamilyFeatures> retrieveAll() throws SQLException, ClassNotFoundException {
    Statement s = db.getStatement();
    ResultSet res = s.executeQuery("select * from family_features");
		
    ArrayList<FamilyFeatures> comb = new ArrayList<FamilyFeatures>();
    
    while(res.next()) {
      FamilyFeatures sf = new FamilyFeatures();
      sf.setFeatureID(res.getInt("FeatureID"));
      sf.setFamilyName(res.getString("FamilyName"));
      comb.add(sf);
    }
    
    return comb;
  }
}
